var mxz = {};
(function($) {
	mxz.shoppingCart = new (function() {
		var updatePath_ = g_userDomain + "/shopping/updateDo/json";
		var buyCountChangeAfter_ = function(){};
		var bCountBtEvent_ = function(event) {
			var $bCountBt = $(event.currentTarget);
			var vars = $bCountBt.attr("id").split("_");
			var $buyCount = $("#bCount_" + vars[2] + "_" + vars[3]);
			var preCount = parseFloat($buyCount.val());
			if (isNaN(preCount)) {
				showMsg("请填写数字！", "error");
				return;
			}
			$buyCount.val(preCount);
			var $datas = $("[id^='data_goods_" + vars[2] + "_" + vars[3] + "']");
			var datas = $datas.attr("id").split("_");
			
			// 减
			if (vars[1] == "m") {
				var destCount = preCount - parseFloat(datas[5]);
				if (destCount < datas[4]) {
					destCount = datas[4];
				}
				if(destCount.toString().indexOf(".") > 0){
					// 小数计算保留一位小数
					var destCountF = destCount.toFixed(1);
					$buyCount.val(destCountF);
				} else {
					$buyCount.val(destCount);
				}
			
			} // 加
			else {
				var destCount = preCount + parseFloat(datas[5]);
				if(destCount.toString().indexOf(".") > 0){
					// 小数计算保留一位小数
					var destCountF = destCount.toFixed(1);
					$buyCount.val(destCountF);
				} else {
					$buyCount.val(destCount);
				}
			}
			$buyCount.trigger("change");
		};
		var bCountChangeEvent_ = function(event) {
			var $buyCount = $(event.currentTarget);
			var vars = $buyCount.attr("id").split("_");
			var buyCount = parseFloat($buyCount.val());
			if (isNaN(buyCount)) {
				showMsg("请填写数字！", "error");
				return;
			}
			$buyCount.val(buyCount);
			var $datas = $("[id^='data_goods_" + vars[1] + "_" + vars[2] + "']");
			var datas = $datas.attr("id").split("_");
			
			// 已购买数
			var hbCount = parseFloat($datas.find("#hbCount_" + vars[1]).html());
			// 剩余数
			var remain = parseFloat(datas[12]) - hbCount;
			if (remain <= 0) {
				buyCount = 0;
				$buyCount.val(0);
				showMsg("已无可购买库存！", "error");
			} else if (buyCount <= 0) {
				buyCount = 0;
				showMsg("购买量不能为0", "error");
			} else if (hbCount >  -1 && remain <  buyCount) {
				buyCount = remain;
				$buyCount.val(remain);
				var msg = "库存不足！最多还可购买：" + remain + datas[8];
				showMsg(msg, "error");
			}
			
			// 是在购物车商品列表，则直接将值更新为购买量
			if (vars[2] == "cart") {
				$.post(updatePath_, {shoppingId: vars[1], buyCount: buyCount}, function(data) {
					if (data.status != 0) {
						showMsg("操作失败！请刷新页面试试。", "error");
					} else {
						buyCountChangeAfter_();
					}
				});
			} else {
				buyCountChangeAfter_();
			}
			
		};
		var bCountBlurEvent_ = bCountChangeEvent_;
		var shoppingCart_ = {
				buyCountChangeAfter : function(callback) {
					if (callback) {
						buyCountChangeAfter_ = callback;
					} else if (buyCountChangeAfter_) {
						buyCountChangeAfter_();
					}
				}
			};
		// 事件:加/减购买量
		$(document).on("click", "[id^='bCountBt_']", bCountBtEvent_);
		// 事件:输入
		$(document).on("blur", "[id^='bCount_']", bCountBlurEvent_);
		// 事件:购买量变化
		$(document).on("change", "[id^='bCount_']", bCountChangeEvent_);
		return shoppingCart_;
	})();
})(jQuery);